<template>
    <div>
        <h3>优惠券核销列表</h3>
        <input type="button" value="导出" @click="daochu" class="btn btn-priamry">
        <hr>
        <input type="button" value="批量删除" @click="delall" class="btn btn-danger">
        <input type="button" value="批量启用" @click="updall(1)" class="btn btn-primary">
        <input type="button" value="批量禁用" @click="updall(2)" class="btn btn-primary">
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>
                        <input type="checkbox" @click="headall" v-model="checkbox">
                    </td>
                    <td>门店名称</td>
                    <td>用户名称</td>
                    <td>优惠券标题</td>
                    <td>使用时间</td>
                    <td>状态</td>
                </tr>

                <tr v-for="item in data.list">
                    <td>
                        <input type="checkbox" @change="bodyall" :value="item.Id" v-model="ids">
                    </td>
                    <td>{{ item.ShopName }}</td>
                    <td>{{ item.UesrName }}</td>
                    <td>{{ item.Title }}</td>
                    <td>{{ item.UseTime }}</td>
                    <td>{{ item.State == 1 ? "启用" : "禁用" }}</td>
                   
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, useSlots, resolveDirective } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import moment from 'moment';
import axios from 'axios';
import { couldStartTrivia } from 'typescript';
const route = useRoute();
const router = useRouter();

let data: any = reactive({
    list: []
})

let checkbox = ref();
let ids = ref([]);

const daochu=()=>{
    location.href=`https://localhost:7035/api/Disc/Daochu`;
}

//批量修改
const updall=(state:any)=>{
    if(ids.value.length<1){
        alert('至少修改一项');
        return;
    }
    axios({
        method:"get",
        url:"/api/Disc/Upd",
        params:{
            ids:ids.value.toString(),
            state:state
        }
    })
    .then(res=>{
        if(res.data>0){
            alert('修改成功');
            checkbox.value="";
            ids.value=[];
            getDiccountcount();
        }
    })
    .catch(err=>{
        console.log(err);
    })
}


//批量删除
const delall = () => {
    //ids.value.toString();
    if(ids.value.length<1){
        alert('至少选中一项');
        return;
    }
    if (confirm('确定删除吗')) {
        axios({
            method: "delete",
            url: "/api/Disc/Del",
            params: {
                ids: ids.value.toString()
            }
        })
            .then(res => {
                if (res.data > 0) {
                    alert('删除成功')
                    checkbox.value="";
                    ids.value=[];
                    getDiccountcount();
                }
            })
            .catch(err => {
                console.log(err);
            })
    }

}


//全选
const headall = () => {
    checkbox.value = !checkbox.value;
    if (checkbox.value == true) {
        ids.value = data.list.map((item: any) => {
            return item.Id
        })
    }
    else {
        ids.value = [];
    }
}
//反选
const bodyall = () => {
    if (ids.value.length == data.list.length) {
        checkbox.value = true;
    }
    else {
        checkbox.value = false;
    }
}

onMounted(() => {
    getDiccountcount();
})
const getDiccountcount = () => {
    axios({
        method: "get",
        url: "/api/Disc/GetDiscountCountList"
    })
        .then(res => {
            data.list = res.data
        })
        .catch(err => {
            console.log(err);
        })
}



</script>






<style scoped></style>